package net.zabuchy.user.action;
 
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.FacesRequestAttributes;
import org.springframework.web.context.request.FacesWebRequest;
import org.springframework.web.jsf.FacesContextUtils;
 
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
 

public class UserAction extends  ActionSupport implements ServletRequestAware, ServletResponseAware{ 
 
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(UserAction.class.getName());
 
    private List users;
 
    private Integer userId;
    private String username;
	private int customerId;
    private String password;
    private String enabled;
    
    @Autowired
    private UserBo userBo;
    
    @Autowired
    private SessionHelper sessionBean;
 
    // Tylko chwilowo 
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    
    
 
    
    public String listAll() {
    	
    	//pobieranie sesji
    	Map session = ActionContext.getContext().getSession();
    	int projectId = 0;
    	
    	try {
    		projectId = (Integer) session.get(ParamNames.CURRENT_PROJECT_ID);
    	} catch (Exception e) {
    		//
    	}
    	LOGGER.debug("UserAction.session.getProjectId " + String.valueOf(projectId));
    	LOGGER.debug("UserAction.session.getCustomerId " + String.valueOf(sessionBean.getCustomerId()));
    	
    	
    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    	Collection<GrantedAuthority> grantedAuthority = auth.getAuthorities();
    	Iterator it = grantedAuthority.iterator();
    	GrantedAuthority grant;
    	while(it.hasNext()) {
    		grant = (GrantedAuthority) it.next();
    		LOGGER.debug("granted authority : " + grant.getAuthority());
    	}
    	LOGGER.debug("loggedIn username: " + auth.getName());
    	/*User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        LOGGER.debug("loggedIn username: " + user.getUsername());*/
        if (userBo == null) {
            return Action.ERROR;
        }
        LOGGER.debug("Get all users");
        users = userBo.findAll();
        LOGGER.debug("Users number = " + users.size());
        User user = userBo.findByUsername(auth.getName());
        userBo.getUserRole(user);
        return Action.SUCCESS;
    }
 
    public String delete() {
        if (userBo == null) {
            return Action.ERROR;
        }
 
        User user = getUser(userId);
        if (user == null) {
            return Action.ERROR;
        }
 
        LOGGER.debug("Delete user " + user.getUsername() + " with id " + user.getUserId());
        userBo.delete(user);
        return Action.SUCCESS;
    }
 
    public String add() {
        if (userBo == null) {
            return Action.ERROR;
        }
 
        User user = new User();
        user.setUsername(username);
        user.setCustomerId(customerId);
        user.setPassword(password);
        user.setEnabled(true);
        LOGGER.debug("Add user: " + user.getUsername());
        userBo.add(user);
        return Action.SUCCESS;
    }
 
    public String execute() {
      return Action.SUCCESS;
    }
 
 
    private User getUser(Integer id) {
        LOGGER.debug("Get user with id = " + id);
        return userBo.findById(id);
       
    }

    public UserBo getUserBo() {
        return userBo;
    }
 
    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }
 
    public List getUsers() {
        return users;
    }
 
    public void setUsers(List users) {
        this.users = users;
    }
 
    public Integer getUserId() {
        return userId;
    }
 
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
 
    public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
    
	public void setEnabled(String enabled) {
		this.enabled = enabled;
	}

	public String getEnabled() {
		return enabled;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}
}